﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using SharedGenomics.Workbench.Core;
using SharedGenomics.Domain;

namespace SharedGenomics.Workbench.PresentationModel
{
    public class JobSubmissionPTO : PresentationTransferObject
    {
        public int? SelectedDataSetId
        {
            get { return this.GetPropertyValue<int?>("SelectedDataSetId"); }
            set { this.SetPropertyValue("SelectedDataSetId", value); }
        }

        public short? SelectedPermutationCount
        {
            get { return this.GetPropertyValue<short?>("SelectedPermutationCount"); }
            set { this.SetPropertyValue("SelectedPermutationCount", value); }
        }

        public IEnumerable<short> AvailablePermutationCounts { get; set; }

        public JobType? SelectedJobType
        {
            get { return this.GetPropertyValue<JobType?>("SelectedJobType"); }
            set { this.SetPropertyValue("SelectedJobType", value); }
        }

        public IEnumerable<ValueDisplayItem<JobType>> JobTypes
        {
            get { return this.GetPropertyValue<IEnumerable<ValueDisplayItem<JobType>>>("JobTypes"); }
            set
            {
                //put a dummy informational job type option on the front of the given sequence - this is not a valid option and if it is selected, validation 
                //will fail (see the Validate method)
                var jobTypes = new[] { new ValueDisplayItem<JobType>((JobType)(-1), "Please select a PLINK function") }.Concat(value ?? Enumerable.Empty<ValueDisplayItem<JobType>>());

                this.SetPropertyValue("JobTypes", jobTypes);
            }
        }

        public IEnumerable<ValueDisplayItem<string>> Phenotypes
        {
            get { return this.GetPropertyValue<IEnumerable<ValueDisplayItem<string>>>("Phenotypes"); }
            set { this.SetPropertyValue("Phenotypes", value); }
        }

        public IEnumerable<ValueDisplayItem<string>> StratificationColumns
        {
            get { return this.GetPropertyValue<IEnumerable<ValueDisplayItem<string>>>("StratificationColumns"); }
            set { this.SetPropertyValue("StratificationColumns", value); }
        }

        public string SelectedPhenotype
        {
            get { return this.GetPropertyValue<string>("SelectedPhenotype"); }
            set { this.SetPropertyValue("SelectedPhenotype", value); }
        }

        public string Description
        {
            get { return this.GetPropertyValue<string>("Description"); }
            set { this.SetPropertyValue("Description", value); }
        }

        protected override void Validate(SharedGenomics.Core.IValidationErrorCollection validationErrors)
        {
            if (!this.SelectedDataSetId.HasValue)
            {
                validationErrors.Add("No data set selected", this, "SelectedDataSetId");
            }

            if (!this.SelectedPermutationCount.HasValue)
            {
                validationErrors.Add("No number of permutations selected", this, "SelectedPermutationCount");
            }

            if (!(this.SelectedJobType.HasValue && ((int)this.SelectedJobType.Value != -1)))
            {
                validationErrors.Add("No job type selected", this, "SelectedJobType");
            }

            if (this.SelectedPhenotype == null)
            {
                validationErrors.Add("Analysis variable required", this, "SelectedPhenotype");
            }
        }
    }
}
